package com.fh.service.impl;

import com.fh.client.OrderSubmitClient;
import com.fh.client.OrderitemClient;
import com.fh.client.ShopCartDelClient;
import com.fh.client.StockUpdateClient;
import com.fh.model.OrderItem;
import com.fh.model.Orders;
import com.fh.model.ProductSku;
import com.fh.model.vo.ShoppingCartVO;
import com.fh.service.OrderSubmitService;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.math.BigDecimal;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.*;

@Service
public class OrderSubmitServiceImpl  implements OrderSubmitService {

     @Resource
     private OrderSubmitClient orderSubmitClient;
     @Resource
     private OrderitemClient orderitemClient;
     @Resource
     private StockUpdateClient stockUpdateClient;
    @Resource
  private ShopCartDelClient shopCartDelClient;
    @Override
    public Map<String, String> addOrders(String cids, Orders orders) {
        Map<String,String> map=new HashMap<>();
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
        String orderId= UUID.randomUUID().toString().replace("-","");
        orders.setOrderId(orderId);
        List<ShoppingCartVO> shoppingCartVOS = orderSubmitClient.addOrder(orders, cids);
        boolean stock=true;
        String  untitled="";
        for (ShoppingCartVO cartVO : shoppingCartVOS) {
            if (Integer.parseInt(cartVO.getCartNum())>cartVO.getStock()){
                stock=false;
                break;
            }
            untitled=untitled+cartVO.getProductName()+",";
        }
             if (shoppingCartVOS!=null){
                 List<OrderItem> orderItems=new ArrayList<>();
                 for (ShoppingCartVO sc : shoppingCartVOS) {
                     String orderItemId= UUID.randomUUID().toString();
                     OrderItem orderItem=new OrderItem();
                     orderItem.setItemId(orderItemId);
                     orderItem.setOrderId(orderId);
                     orderItem.setProductId(sc.getProductId());
                     orderItem.setProductPrice(new BigDecimal(sc.getSellPrice()));
                     orderItem.setBuyCounts(Integer.parseInt(sc.getCartNum()));
                     orderItem.setSkuId(sc.getSkuId());
                     orderItem.setProductImg(sc.getProductImg());
                     orderItem.setTotalAmount(new BigDecimal(sc.getSellPrice()));
                     orderItem.setSkuName(sc.getSkuName());
                     orderItem.setBuyTime(new Date());
                     orderItem.setIsComment(0);
                     orderItem.setProductName(sc.getProductName());
                     try {
                         orderItem.setBasketDate(sdf.parse(sc.getCartTime()));
                     } catch (ParseException e) {
                         e.printStackTrace();
                     }
                     int i = orderitemClient.addOrderitem(orderItem);
                     if (i>0){
                         for (ShoppingCartVO cartVO : shoppingCartVOS) {
                             int newStock=cartVO.getStock()-Integer.parseInt(cartVO.getCartNum());
                             ProductSku productSku=new ProductSku();
                             productSku.setSkuId(cartVO.getSkuId());
                             productSku.setStock(newStock);
                             int update = stockUpdateClient.update(productSku);
                             if (update>0){
                                 List<Integer> list=new ArrayList<>();
                                 String[] arr = cids.split(",");
                                 for (String cid : arr) {
                                     list.add(Integer.parseInt(cid));
                                 }
                                 for (Integer cid : list) {
                                     shopCartDelClient.del(cid);
                                 }
                                 map.put("orderId",orderId);
                                 map.put("productName",untitled.substring(0,untitled.length()-1));
                                 return  map;
                             }

                         }
                     }

                 }
             }

        return null;
    }
}
